package com.itheima;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyBatisTestUser {

    @Test
    public void testFindUserById() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User userById = userMapper.findUserById(1);
        System.out.println("userById = " + userById);
        sqlSession.close();
    }


    @Test
    public void testDeleteUserById() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        userMapper.deleteUserById(11);
        sqlSession.commit();
        sqlSession.close();

    }

    @Test
    public void testUpdateUserById() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        //User user = userMapper.findUserById(10);
        User user = new User();

        user.setId(4);
        user.setUsername("张张张");
        user.setSex("女");
        user.setAddress("津安创意园");
        userMapper.updateUserById(user);

        sqlSession.commit();
        sqlSession.close();

    }

    @Test
    public void testAddUser() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();

        user.setUsername("黑马程序员");
        user.setBirthday(Date.valueOf("1999-09-12"));
        user.setSex("男");
        user.setAddress("津安创意园");
        userMapper.addUser(user);
        System.out.println("添加数据后： " + user);
        sqlSession.commit();
        sqlSession.close();

    }


    @Test
    public void testFindUserByCondition() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        String username = "黑";
        String sex = "男";
        username = username+"%";
        List<User> userByCondition = userMapper.findUserByCondition(username, sex);
        System.out.println("userByCondition = " + userByCondition);
        sqlSession.close();
    }
    @Test
    public void testFindUserByUser() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();
//        user.setUsername("张三");
        user.setSex("女");

        List<User> userByUser = userMapper.findUserByUser(user);

        System.out.println("userByUser = " + userByUser);

        sqlSession.close();
    }

    @Test
    public void testDeleteByIds() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        int[] ids = {10,12,13};

        userMapper.deleteByIds(ids);

        sqlSession.close();
    }


}
